package com.zzy.service.impl;


import com.zzy.domain.Login;
import com.zzy.domain.vo.LoginInfoVo;
import com.zzy.mapper.LoginMapper;
import com.zzy.service.LoginService;
import com.zzy.util.JwtUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class LoginServiceImpl implements LoginService {
    private final LoginMapper loginMapper;

    @Override
    public LoginInfoVo login(Login login) {
        // 创建User对象用于查询
        Login logins = new Login();
        logins.setUsername(login.getUsername());
        logins.setPassword(login.getPassword());

        Login userLogin = loginMapper.getnameAndPassword(logins);
        if(userLogin != null){
            //1.生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", userLogin.getId());

            dataMap.put("username", userLogin.getUsername());
            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfoVo loginInfoVo = new LoginInfoVo(userLogin.getId(), userLogin.getUsername(), jwt);
            return loginInfoVo;
        }
        return null;
    }
}
